package com.zxl.test.domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.zxl.test.Get51job;

public class CollectWord {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Long st = System.currentTimeMillis();
		
		List list = new ArrayList();
		StringBuffer sb = new StringBuffer();
		/*
		//读取关键词文件
		list = readFile("E:\\workspace\\zxl-test\\src\\com\\zxl\\test\\domain\\words1.txt");
		
		//并获得每个词的全拼及首字母
		ChineseToEnglish cte = new ChineseToEnglish();
		for(int i = 0; i < list.size(); i++){
			sb.append(cte.getPingYin(list.get(i).toString()) + "\r\n");
			sb.append(cte.getPinYinHeadChar(list.get(i).toString()) + "\r\n");
		}
		
		//写到文件中
		Get51job gj = new Get51job();
		gj.writeFile("E:\\workspace\\zxl-test\\src\\com\\zxl\\test\\domain\\words2.txt", sb);
		
		sb.setLength(0);
		*/
		
		list = readFile("E:\\workspace\\zxl-test\\src\\com\\zxl\\test\\domain\\words2.txt");
		SearchDomain sd = new SearchDomain();
		for(int j = 0; j < list.size(); j++){
			System.out.println(list.get(j).toString());
//			try {
//				sd.testPost(list.get(j).toString());
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}
		
		
		int s = list.size();
		Random r = new Random();
		for(int z = 0; z < 10; z++){
//			System.out.println(list.get(r.nextInt(s)) + "-" + list.get(r.nextInt(s)));
			try {
				sd.testPost(list.get(r.nextInt(s)) + "-" + list.get(r.nextInt(s)));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		Long et = System.currentTimeMillis();
		System.out.println("共耗时" + (et-st)/1000 + "秒");
	}

	/**
	 * 读取文件
	 * @param filepath
	 */
	public static List readFile(String filepath){
//		StringBuffer sb = new StringBuffer();
		List<String> list = new ArrayList<String>();
		
		File file = new File(filepath);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			// 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                if(tempString.startsWith("#")){
                	continue;
                }else{
//                	System.out.println(tempString);
//                	sb.append(cte.getPingYin(tempString) + "\r\n");
//                	sb.append(cte.getPinYinHeadChar(tempString) + "\r\n");
                	list.add(tempString);
                }
            }
            reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            if (reader != null) {
            	try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        }
        
        return list;
	}
	
}
